/**
 * 搜索页面的js
 */

var pageNo = 1; //当前页码
var limit = 10; //每页的新闻数量

 //查询
 function search(columnid){
	var title=$("#searchValue").val();
	if(title==undefined)
	{
		title="";
	}
	else{
		title = trim(title); //删除左右两端的空格
		title = encodeURI(encodeURI(title));//转义传输
	}
 	var paramData = {};
 	paramData["pageNo"] = pageNo;
 	paramData["limit"] = limit;
 	paramData["title"] = title;
 	paramData["state"] = 3;
 	if(columnid != "-1"){
 		paramData["newsColumns.id"] = columnid;
 	}
 	//获取查询数据
 	$.post(realPath+"/path/newsinfo/listNewsinfoForJsonTwo", paramData,
 			function(result) {
 				if (result.state == 'Success') {
 					//显示新闻列表
 					showNewsList(result.content, title);
 				} else {
 					alert("操作失败,原因："+result.content);
 				}
 			}
 		);
 }
 /**
  * 去除字符串首尾空格
  */
 String.prototype.trim=function(){
	 return this.replace(/(^\s*)|(\s*$)/g, "");
 }
 //显示新闻列表
 function showNewsList(data, searchValue){
	 var searchValue=$("#searchValue").val();
	 if(searchValue.trim().length==0){
		 searchValue="";
	 }
 	//显示搜索结果
 	$("#searchKeyResult").html("搜索<em>" + searchValue + "</em>的结果");
 	//显示新闻详情
 	var content = "<ul>";
 	for(var i=0; i<data.items.length; i++){
 	 	var newsUrl = realPath + "/path/newsinfo/frontNewsInfo?newsId=" + data.items[i]["id"];
 		content += "<li><div class=\"mod-resultList-abstract clearfix\"><div class=\"img\"><a href=\"" + newsUrl + "\" target=\"_blank\">";
 		content += "<img src=\"" + realPath + data.items[i]["coverPath_mmseg4j"] + "\" width=\"\" height=\"\">";
 		content += "</a></div><div class=\"mod-resultList-hd\"><h3 class=\"mod-resultList-title\">";
// 		content += "<a href=\"" + newsUrl + "\" target=\"_blank\">" + changeKeyWordStytle(data.items[i]["title"], searchValue) + "</a></h3></div>";
 		content += "<a href=\"" + newsUrl + "\" target=\"_blank\">" +data.items[i]["title_mmseg4j"]+ "</a></h3></div>";
 		content += "<div class=\"mod-resultList-tags\">";
// 		var keywords = splitKeyWord(data.items[i]["keyword"]); //调用通用的拆分方法 common.js
// 		for (var key in keywords){
// 	 	 	var searchUrl = realPath + "/path/newsinfo/search?value=" + keywords[key];
// 			content += " <a href=\"" + searchUrl + "\" title=\"" + keywords[key] + "\" target=\"_blank\">" + keywords[key] + "</a>";
// 		}
 		var searchUrl = realPath + "/path/newsinfo/search?value=" + encodeURI(encodeURI(data.items[i]["keyword_mmseg4j"]));//中文转义
 		content += " <a href=\"" + searchUrl + "\" title=\"" + data.items[i]["keyword_mmseg4j"] + "\" target=\"_blank\">"+undefinedOperate(data.items[i]["keyword_mmseg4j_2"],"")+ "</a>";
 		content += "</div><div class=\"mod-resultList-bd\">";
// 		content += "<p class=\"mod-resultList-info\">" + changeKeyWordStytle(data.items[i]["nabstract"], searchValue) + "</p>";
 		content += "<p class=\"mod-resultList-info\">" + undefinedOperate(data.items[i]["nabstract_mmseg4j"],"")+ "</p>";
 		content += "</div></div></li>";
 	}
 	content += "</ul>";
 	$("#newsList").html(content);
 	//分页
 	pagnation(data.totalCount,data.totalPageCount,data.currentPageNo);
 }
 
 //改变内容中关键字的样式
// function changeKeyWordStytle(content, keyword){
// 	if(content == null) return "";
// 	if(keyword == "") return content;
// 	//判断是否包含字符串
// 	var newContent = content;
// 	if(content.indexOf(keyword) > -1){
// 		//拆分字符串
// 		var str=content.split(keyword);
// 		newContent = str[0];
// 		for (var i=1;i<str.length ;i++ )   
// 	    { 
// 			newContent += "<em>" + keyword + "</em>"; //关键字颜色变化
// 			newContent += str[i];
// 	    }
// 	}
// 	return newContent;
// }
 
//显示页面
function pagnation(totalCounts,totalPages,currentPageNo){
	var pagnation = $("#pagnation");
	var pagnationHtml = "";
	var pagePrevHtml = "";
	var pageNextHtml = "";
	var pageNumberHtml = "";
	
	//暂时先不用加载条数记录
	//pagnationHtml += "<span class=\"pageDown-sum ml15\"><em>共<i>"+totalCounts+"</i>条记录</em><em>当前第 <i>"+currentPageNo+"</i>页 </em><i>/</i>共<i> "+totalPages+"</i>页</span>";
	if(totalPages > 0)
	{
		if(currentPageNo == 1)
		{
			pagePrevHtml = "<a class=\"page-prev\" title=\"上一页\">上一页</a>";
			//总页码为 1，禁止下一页
			if(totalPages==1)
			{
				pageNextHtml = "<a class=\"page-next\" title=\"下一页\">下一页</a>";
			}else{
				pageNextHtml = "<a href=\"javascript:goPage("+parseInt(currentPageNo+1)+")\" class=\"page-next\" title=\"下一页\">下一页</a>";	
			}
		}else if(currentPageNo < totalPages){
			pagePrevHtml = "<a href=\"javascript:goPage("+parseInt(currentPageNo-1)+")\" class=\"page-prev\" title=\"上一页\">上一页</a>";
			pageNextHtml = "<a href=\"javascript:goPage("+parseInt(currentPageNo+1)+")\" class=\"page-next\" title=\"下一页\">下一页</a>";
		}else if(currentPageNo == totalPages){
			pagePrevHtml = "<a href=\"javascript:goPage("+parseInt(currentPageNo-1)+")\" class=\"page-prev\" title=\"上一页\">上一页</a>";
			pageNextHtml = "<a class=\"page-next\" title=\"下一页\">下一页</a>";
		}
		//总页码 <7
		if(totalPages < 7){
			for(var i = 0 ; i < totalPages ; i++){
				var currentClass = "";
				if((i+1) == currentPageNo){
					currentClass = "class = \"current\"";
				}
				pageNumberHtml += "<a href=\"javascript:goPage("+parseInt(i+1)+")\" "+currentClass+">"+(i+1)+"</a>";
			}
		}else{
			//判断是否后几页
			if(totalPages - currentPageNo - 4 > 0){
				var start = 0,end = 0;
				if(currentPageNo  < 3){
					start = 0;end = 6;
				}else{
					start = currentPageNo - 3 ; end = currentPageNo + 3;
				}
				for(var i = start ; i < end ; i++){
					var currentClass = "";
					if((i+1) == currentPageNo){
						currentClass = "class = \"current\"";
					}
					pageNumberHtml += "<a href=\"javascript:goPage("+parseInt(i+1)+")\" "+currentClass+">"+(i+1)+"</a>";
				}
				pageNumberHtml += "<span class=\"page-items\">...</span>";
				pageNumberHtml += "<a href=\"javascript:goPage("+totalPages+")\" >"+totalPages+"</a>";
			}else{
				pageNumberHtml += "<a href=\"javascript:goPage(1)\" >1</a>";
				pageNumberHtml += "<span class=\"page-items\">...</span>";
				for(var i =totalPages - 6 ; i < totalPages ; i++){
					var currentClass = "";
					if((i+1) == currentPageNo){
						currentClass = "class = \"current\"";
					}
					pageNumberHtml += "<a href=\"javascript:goPage("+parseInt(i+1)+")\" "+currentClass+">"+(i+1)+"</a>";
				}
			}
		}
		
		pagnation.html( pagePrevHtml + pageNumberHtml + pageNextHtml + pagnationHtml);
	}else{
		pagnation.html("<font color='red' size='+2'>抱歉，没有您要搜索的内容</font>");
	}
	
}

function goPage(number){
	pageNo = number;
	search();
}
function undefinedOperate(str,replaceStr){
	return typeof(str) == "undefined" ? replaceStr : str;
}
